<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Layout {

    var $CI;
    var $layout;
    var $css = array();
    var $js = array();
    var $view ;

    function Layout($layout = "default") {
        //die('Loading Layout Library...');
        $this->CI =& get_instance();
        $this->layout = 'system_layout/'.$layout;
        
    }#end of function'system_layout/'.

    function setLayout($layout) {
        $this->layout = 'system_layout/'.$layout;
    }#end of function

    function view($view, $data=null, $return=false) {
         //die('Rendering Default Layout..');

        $loadedData = array();
        $loadedData['content_for_layout'] = $this->CI->parser->parse($view,$data,true);
        $loadedData['css'] = $this->getCss();
        $loadedData['js'] = $this->getJs();

        // print_r($loadedData);
        if($return) {

            //$output = $this->CI->load->view($this->layout, $loadedData, true);
            $output = $this->CI->parser->parse($this->layout, $loadedData, true);
            return $output;
        }
        else {

            //$this->obj->load->view($this->layout, $loadedData, false);
            $this->CI->parser->parse($this->layout, $loadedData, false);

        }

        
    }#end of function

    private function getCss() {
        $css = "";
        if(!empty($this->css)) {
            $base = $this->CI->config->item('css_dir');
            foreach($this->css as $files) {
                $css .= '<link rel="stylesheet" type="text/css" media="all" href="'.$base."".$files.'.css" />'."\n";
            }
        }
        return $css;
    }#end of function


    private function getJs() {
        $js ='';
        if(!empty($this->js)) {
            $base = $this->CI->config->item('js_dir');
            foreach($this->js as $files) {
                $js .= '<script language="javascript" type="text/javascript" src="'.$base."".$files.'.js" ></script>'."\n";
            }
        }
        return $js;
    }#end of function

}#end of class